package top.forlenet.dao.impl;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import top.forlenet.dao.UserDao;
import top.forlenet.entity.User;
import top.forlenet.utils.DruidUtils;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public void insert(User user) {
        QueryRunner queryRunner = DruidUtils.getQueryRunner();
        String sql = "insert into user(phone,password,pid) values(?,?,?)";
        try {
            queryRunner.update(sql, user.getPhone(), user.getPassword(), user.getPid());
        } catch (SQLException e) {
            throw new RuntimeException("添加用户信息异常");
        }
    }

    @Override
    public User selectByPhone(String phone) {
        QueryRunner queryRunner = DruidUtils.getQueryRunner();
        String sql = "select * from user where phone=?";
        try {
            return queryRunner.query(sql, new BeanHandler<User>(User.class), phone);
        } catch (SQLException e) {
            throw new RuntimeException("添加用户信息异常");
        }
    }
}
